using System;
using System.Collections.Generic;
using System.Text;
using Clanta.Core.Domain;
using Clanta.Core.DataInterfaces;
using ProjectBase.Data;
using NHibernate.Expression;
using NHibernate;

namespace Clanta.Data
{
    public class PresentationDao : AbstractNHibernateDao<Presentation, long>, IPresentationDao
    {
        public PresentationDao(string sessionFactoryConfigPath)
            : base(sessionFactoryConfigPath)
        {
        }

        public List<Presentation> GetPresentationsByConferenceId(long conferenceId)
        {
            List<Presentation> uniqueResult = new List<Presentation>();

            List<Presentation> duplicateResult = NHibernateSession.CreateQuery(
                @"from Presentation p
                  left join fetch p.Track
                  left join fetch p.Speakers
                  where p.Conference.Id = :conferenceId
                  order by p.Track.Name, p.Name")
                .SetInt64("conferenceId", conferenceId)
                .List<Presentation>() as List<Presentation>;

            long prevId = -1;
            foreach (Presentation p in duplicateResult)
            {
                if (p.Id != prevId)
                {
                    uniqueResult.Add(p);
                }
                prevId = p.Id;
            }
            return uniqueResult;
        }
    }
}
